package com.miplus.ccrm.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.admin.controller.vo.DeviceHistReqVo;
import com.miplus.ccrm.admin.controller.vo.DeviceHistResVo;
import com.miplus.ccrm.admin.mapper.SysDeviceHistMapper;
import com.miplus.ccrm.admin.pojo.SysDeviceHist;
import com.miplus.ccrm.admin.service.SysDeviceHistService;
import com.miplus.ccrm.common.CmdResVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author pc
* @description 针对表【sys_audit_log】的数据库操作Service实现
* @createDate 2024-03-07 10:34:19
*/
@Slf4j
@Service
public class SysDeviceHistServiceImpl extends MPJBaseServiceImpl<SysDeviceHistMapper, SysDeviceHist>
    implements SysDeviceHistService {
    @Override
    public  CmdResVo startDevice(String dept, String usrid, String device)
    {
        //
        log.info("start device...dept:{}, usrid:{},device:{}",dept, usrid, device);
        if( StringUtils.isEmpty(device) || StringUtils.isEmpty(usrid))
        {
            return new CmdResVo(-1, "参数不能为空");
        }
//        SysDeviceHist otherDeviceHist = baseMapper.selectOne(new LambdaQueryWrapper<SysDeviceHist>()
//                .eq(SysDeviceHist::getDevice,device)
//                .ne(SysDeviceHist::getUsrid, usrid)
//                .isNull(SysDeviceHist::getEnddate)
//                .last("limit 1")
//        );
//        if(otherDeviceHist != null )
//        {
//            log.info("start device fail。has exists.");
//            return new CmdResVo(-1,"该分机"+device +"已分配给其他催员!");
//        }
        SysDeviceHist sysDeviceHist = baseMapper.selectOne(new LambdaQueryWrapper<SysDeviceHist>()
                .eq(SysDeviceHist::getUsrid, usrid)
                .isNull(SysDeviceHist::getEnddate)
                .last("limit 1")
        );
        if(ObjectUtils.isNotNull(sysDeviceHist) ){
            if(StringUtils.isNotEmpty(sysDeviceHist.getDevice()) && sysDeviceHist.getDevice().equals(device)){
                return new CmdResVo(sysDeviceHist.getId(),"执行成功");
            }
            else{
                sysDeviceHist.setEnddate(new Date());
                updateById(sysDeviceHist);
            }
        }
        SysDeviceHist newDeviceHist = new SysDeviceHist();
        newDeviceHist.setDept(dept);
        newDeviceHist.setUsrid(usrid);
        newDeviceHist.setDevice(device);
        newDeviceHist.setStartdate(new Date());
        save(newDeviceHist);
        log.info("start device OK");
        return new CmdResVo(newDeviceHist.getId(),"执行成功");
    }

    @Override
    public CmdResVo endDevice(String usrid, String device)
    {
        log.info("end device...usrid:{},device:{}",usrid, device);
        if( StringUtils.isEmpty(device) || StringUtils.isEmpty(usrid))
        {
            return new CmdResVo(-1, "参数不能为空");
        }
        SysDeviceHist sysDeviceHist = baseMapper.selectOne(new LambdaQueryWrapper<SysDeviceHist>()
                .eq(SysDeviceHist::getUsrid, usrid)
                .eq(SysDeviceHist::getDevice,device)
                .isNull(SysDeviceHist::getEnddate)
        );
        if(ObjectUtils.isNotNull(sysDeviceHist) ){
            sysDeviceHist.setEnddate(new Date());
            updateById(sysDeviceHist);
        }
        log.info("end device OK");
        return new CmdResVo(1,"执行成功");
    }
    @Override
    public Long checkValidDevice(String usrid, String device)
    {
         return baseMapper.selectCount(new LambdaQueryWrapper<SysDeviceHist>()
                .eq(SysDeviceHist::getDevice,device)
                .ne(SysDeviceHist::getUsrid, usrid)
                .isNull(SysDeviceHist::getEnddate)
        );
    }
    @Override
    public IPage<DeviceHistResVo> getDeviceHistList(DeviceHistReqVo paras, String usrid)
    {
        return selectJoinListPage(
                new Page<>(paras.getPageNum(),paras.getPageSize()),
                DeviceHistResVo.class,
                new MPJLambdaWrapper<SysDeviceHist>()
                        .eq(StringUtils.isNotEmpty(paras.getGrpid()),SysDeviceHist::getDept,paras.getGrpid())
                        .eq(StringUtils.isNotEmpty(paras.getAccount()),SysDeviceHist::getUsrid,paras.getAccount())
                        .eq(StringUtils.isNotEmpty(paras.getDevice()),SysDeviceHist::getDevice,paras.getDevice())
                        .ge(StringUtils.isNotEmpty(paras.getCrdt1()),SysDeviceHist::getStartdate,paras.getCrdt1())
                        .le(StringUtils.isNotEmpty(paras.getCrdt2()),SysDeviceHist::getStartdate,paras.getCrdt2())
                        .ge(StringUtils.isNotEmpty(paras.getEnddt1()),SysDeviceHist::getEnddate,paras.getEnddt1())
                        .le(StringUtils.isNotEmpty(paras.getEnddt2()),SysDeviceHist::getEnddate,paras.getEnddt2())

                        .apply(StringUtils.isNotEmpty(paras.getMon())," t.startdate < date_add(str_to_date('" + paras.getMon()+ "-01 00:00:00','%Y-%m-%d %H:%i:%s'),INTERVAL 1 MONTH)")
                        .apply(StringUtils.isNotEmpty(paras.getMon())," ifnull(t.enddate,now()) >= str_to_date('" +paras.getMon() + "-01 00:00:00','%Y-%m-%d %H:%i:%s')")
                        .selectAll(SysDeviceHist.class)
                        .selectAs("fun_getusername(t.usrid)",DeviceHistResVo::getName)
                        .selectAs(SysDeviceHist::getDept,DeviceHistResVo::getGrpid)
                        .selectAs("(select grpname from sys_grp where grpid = t.dept)",DeviceHistResVo::getGroups)
                        .orderByDesc(SysDeviceHist::getStartdate)
                );
    }
}




